/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Estructuras;

/**
 *
 * @author jose
 */

import java.util.TreeMap;

/*
 * K y T son tipos genericos
 */
public class Tree_RB<K,T> extends TreeMap{

    private K minor_key;
    private T minor_value;

    public Tree_RB(){
        super();
    }

    public K getMinor_key() {
        return minor_key;
    }

    public void setMinor_key(K minor_key) {
        this.minor_key = minor_key;
    }

    public T getMinor_value() {
        return minor_value;
    }

    public void setMinor_value(T minor_value) {
        this.minor_value = minor_value;
    }



    public synchronized void add(K key, T element) {
        super.put(key, element);
        // Se busca el proceso con el menor vruntime
        K minorkey = (K)this.firstKey();
        this.minor_key = minorkey;
        this.minor_value = (T)this.get(minorkey);
    }

    public synchronized void delete(K key){
        super.remove(key);
    }

    /**
     * Busca el elemento con la menor clave y lo elimina del arbol.
     * Cuando el arbol esta vacio devuelve null.
     * @return
     */
    public synchronized T getMinor(){
        
        this.remove(this.minor_key);
        T value = this.minor_value;
        K minorkey = (K)this.firstKey();
        this.minor_key = minorkey;
        this.minor_value = (T)this.get(minorkey);
        return value;


    }

    public synchronized T getMajor(){
        K key = (K)this.lastKey();
        return (T)this.get(key);
    }
}
